const utils = {
	isDateString(str) {
		const reg1 = /^\d{4}[-/]\d{1,2}[-/]\d{1,2}$/
		const reg2 = /^\d{4}[-/]\d{1,2}[-/]\d{1,2}[\sT]\d{1,2}:\d{1,2}(:\d{1,2})*$/
		return reg1.test(str) || reg2.test(str)
	},
	fill(str) {
		return +str > 9 ? String(str) : `0${str}`
	}
}

class Dayjs {
	constructor(arg) {
		this._day
	    this.__init(arg)
	}
	
	__init(arg) {
		if (!arg) {
			this._day = new Date()
		} else if (utils.isDateString(arg)) {
			this._day = new Date(arg.replace(/-/g, '/'))
		}
	}
	
	getDate() {
		return this._day
	}
	
	format(partten = 'YYYY-MM-DD') {
		const d = this._day

		const fullYear = d.getFullYear()
		const year = d.getFullYear().toString().substr(2, 2)
		const fullMonth = utils.fill(d.getMonth() + 1)
		const month = fullMonth.substr(1, 1)
		const fullDate = utils.fill(d.getDate())
		const date = fullDate.substr(1, 1)
		
		const flags = {
			'YYYY': fullYear,
			'YY': year,
			'MM': fullMonth,
			'M': month,
			'DD': fullDate,
			'D': date
		}
		let ret = partten
		Object.keys(flags).forEach(flag => {
			ret = ret.replace(new RegExp(flag, 'g'), flags[flag])
		})
		return ret
	}
	
	isValid() {
		return true
	}
	
	daysInMonth() {
		return new Date(this._day.getFullYear(), this._day.getMonth() + 1, 0).getDate()
	}
	
	date(num) {
		if (!isNaN(num) && num !== null && num !== undefined) {
			this._day.setDate(num)
			return this
		} else {
			return this._day.getDate()
		}
	}
}

export default function(args) {
	return new Dayjs(args)
}